package main

import "fmt"

// 分支与循环
func main() {
	var grade string = "B"
	var marks int = 50
	switch marks {
	case 90:
		grade = "A"
	case 80:
		grade = "B"
	case 60, 70:
		grade = "C"
	case 50:
		grade = "D"
	default:
		grade = "F"
	}
	fmt.Printf("你的等级是 %s\n", grade)

	//switch 还可以使用if...else作为case条件
	age := 7
	switch {
	case age >= 6 && age <= 12:
		fmt.Println("It's primary school")
	case age >= 13 && age <= 15:
		fmt.Println("It's middle school")
	case age >= 16 && age <= 18:
		fmt.Println("It's high school")
	default:
		fmt.Println("The age is unkown")
	}

	//小技巧：使用Switch对interface{}进行断言，例如：
	checkType(32)

	//使用for来循环操作
	for i := 0; i < 2; i++ {
		fmt.Println("for runloop ", i)
	}

	//for..range对数组、切片、map、字符串等进行循环操作
	//对数组操作
	numbers := []int{1, 2, 3}
	for i, v := range numbers {
		fmt.Printf("nnumbers[%d] is %d \n", i, v)
	}

	//对map操作
	cityCodes := map[string]int{
		"北京": 1,
		"上海": 2,
	}
	for i, v := range cityCodes {
		fmt.Printf("cityCodes key[%s] is %d \n", i, v)
	}

	AnotherNumbers := []int{1, 2, 3, 4, 5, 6}
	for i, v := range AnotherNumbers {
		if v == 6 {
			break
		}

		if v%2 == 0 {
			continue
		}
		//break会结束所有循环
		//continue会跳过当前循环直接进入下一次循环
		fmt.Printf("break & continue [%d] is %d \n", i, v)
	}

	outerFunc()
}

// 使用Switch对interface{}进行断言，判断数据类型Type
func checkType(i interface{}) {
	switch v := i.(type) {
	case int:
		fmt.Printf("%v is an int \n", v)

	case string:
		fmt.Printf("%v is an string \n", v)

	case float32:
		fmt.Printf("%v is an float32 \n", v)

	default:
		fmt.Println("The type is unkown")
	}
}
